package com.chuanke.ckfamily.service.user.impl;


import java.io.IOException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.http.ParseException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSONObject;
import com.chuanke.ckfamily.dao.common.model.TeamCreateLog;
import com.chuanke.ckfamily.dao.social.model.Family;
import com.chuanke.ckfamily.dao.social.model.FamilyMember;
import com.chuanke.ckfamily.dao.user.UserDao;
import com.chuanke.ckfamily.dao.user.model.User;
import com.chuanke.ckfamily.service.common.TeamCreateLogService;
import com.chuanke.ckfamily.service.common.WyService;
import com.chuanke.ckfamily.service.constant.Constant;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.social.FamilyMemberService;
import com.chuanke.ckfamily.service.social.FamilyService;
import com.chuanke.ckfamily.service.user.UserService;
import com.chuanke.ckfamily.service.util.CkServerUtil;
import com.weijie.core.util.WJUtils;
import com.weijie.core.wy.WyHelper;
import com.weijie.core.wy.model.TeamJoinTeams;
import com.weijie.core.wy.response.WyTeamJoinTeamsResponse;
import com.weijie.corel.wy.request.WyTeamJoinTeamsRequest;
import com.weijie.corel.wy.request.WyTeamQueryRequest;
import com.weijie.corel.wy.request.WyTeamUpdateRequest;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:META-INF/spring/*-spring.xml" })
public class CreateKefuTestImpl {
	
	 private static final DecimalFormat userNoFormat = new DecimalFormat("dcf00000000");
	
	@Autowired
	private WyService wyService;
	
	@Autowired
	private TeamCreateLogService teamCreateLogService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private FamilyService familyService;
	
	@Autowired
	private FamilyMemberService familyMemberService;
	
	@Autowired
	private UserDao userDao;
	
	
//	家族管理员	登录账号	家族名称	uid
//	卢永梅	18955366163		70
//	肖瑞	18255181863	荔枝团子	110165
//	王婷	13359080205	赚他一个亿	52
//	何小娟	18010739455	钻石家族	64
//	胡用洋	17755342163	皇亲国戚	158835
//	许雅婷	13955393869	名门望族	26526
//	朱成毅 	17775257920	轻松赚钱	60
//	魏榕	15155362101	黄金矿工	673997
//	何晓婉	17756519455	网赚之家	286
//	王建峰	13610916099	赚啊赚啊赚	62
//	余金金	18655333863	传客大师	734030
//	王军	13307166750	快乐赚钱	725454
//	魏建功	13901049956	转发客联盟	725493
//	陶梦	18355381863	乱舞春秋	712614
//	客服部	13800138000	赚钱先锋	726657
//	客服部	13800138001	传客分队	732291
//	客服部	13800138002	日进斗金	732333
//	客服部	13800138003	红包不停	732336
//	客服部	13800138004	达官贵人	732339
//	客服部	13800138007	赚客大师	740921

	@Test
	public void initHead() throws ParseException, CFException, IOException {
		List<kefu> list=new ArrayList<>();
		list.add(new kefu(70, "晨曦家族"));
		list.add(new kefu(110165, "荔枝团子"));
		list.add(new kefu(52, "赚他一个亿"));
		list.add(new kefu(64, "钻石家族"));
		list.add(new kefu(158835, "皇亲国戚"));
		list.add(new kefu(26526, "名门望族"));
		list.add(new kefu(60, "轻松赚钱"));
		list.add(new kefu(673997, "黄金矿工"));
		list.add(new kefu(286, "网赚之家"));
		list.add(new kefu(62, "赚啊赚啊赚"));
		list.add(new kefu(734030, "传客大师"));
		list.add(new kefu(725454, "快乐赚钱"));
		list.add(new kefu(725493, "转发客联盟"));
		list.add(new kefu(712614, "乱舞春秋"));
		list.add(new kefu(726657, "赚钱先锋"));
		list.add(new kefu(732291, "传客分队"));
		list.add(new kefu(732333, "日进斗金"));
		list.add(new kefu(732336, "红包不停"));
		list.add(new kefu(732339, "达官贵人"));
		list.add(new kefu(740921, "赚客大师"));

		System.out.println(list.size());
		
		List<TeamCreateLog> teams=teamCreateLogService.findTeamCreateLog();
		for (TeamCreateLog teamCreateLog : teams) {
			try {
				wyService.removeTeam(teamCreateLog.getTid(), teamCreateLog.getOwner_user_no());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		for (kefu kf : list) {
			Long uid=kf.getId().longValue();
			String fname=kf.getFname();
			User tuser = userDao.findById(uid);
			if(tuser!=null){
				System.out.println("用户已经存在。uid="+uid);
				continue;
			}
			
			User user = CkServerUtil.loadUser(uid);
			if(user==null){
				System.out.println("用户创客不存在。uid="+uid);
				continue;
			}
			String userNo = userNoFormat.format(uid);
			
			user.setIs_admin(1);
			user.setIs_header(1);
			user.setUser_no(userNo);
			
			String wy_token = wyService.registUser(user);
			if(wy_token==null){
				System.out.println("注册云信失败。uid="+uid);
				continue;
			}
			user.setWy_token(wy_token);
			userService.save(user);
			
			Family family=new Family();
			family.setAnnouncement("");
			family.setBalance(0l);
			family.setCreate_time(new Timestamp(System.currentTimeMillis()));
			family.setUpdate_time(new Timestamp(System.currentTimeMillis()));
			family.setFicon(user.getPic());
			family.setIntro("");
			family.setName(fname);
			family.setType(2);
			family.setOwner_user_id(user.getId());
			family.setOwner_user_no(user.getUser_no());
			family.setSuper_id(user.getSuper_id());
			family.setFcapacity(500);
			family.setMnum(1);
			
			String tid = wyService.createTeam(family);
			if(tid==null){
				System.out.println("创建云信群失败。uid="+uid);
			}
			family.setTid(tid);
			
			familyService.save(family);
			
			
			FamilyMember fm = new FamilyMember();
			fm.setFamily_id(family.getId());
			fm.setDevote(0l);
			fm.setDevote_sum(0l);
			fm.setIs_delete(0);
			fm.setMute(0);
			fm.setMute_num(null);
			fm.setMute_time(null);
			fm.setNick_name(user.getNick_name());
			fm.setUser_id(user.getId());
			fm.setUpdate_time(new Timestamp(System.currentTimeMillis()));
			fm.setCreate_time(new Timestamp(System.currentTimeMillis()));
			fm.setNocall(0);
			fm.setVersion("");
			familyMemberService.save(fm);
			
			wyService.addTeamMember(family, user.getUser_no());
		}
	
	}
	
	class kefu{
		private Integer id;
		private String fname;
		
		public kefu(Integer id, String fname) {
			super();
			this.id = id;
			this.fname = fname;
		}
		public Integer getId() {
			return id;
		}
		public void setId(Integer id) {
			this.id = id;
		}
		public String getFname() {
			return fname;
		}
		public void setFname(String fname) {
			this.fname = fname;
		}
		
		
	}
	
	

	

}